﻿using UnityEngine;
using System.Collections;

/// <summary>
/// audio clip with name.
/// </summary>
[System.Serializable]
public class wsAudioClip
{
	public string name;
	public AudioClip clip;
}

/// <summary>
/// Audio Manager
/// </summary>
[AddComponentMenu("wsLibrary/Audio/Audio Manager")]
[RequireComponent(typeof(AudioSource))]
public class wsAudioManager : MonoBehaviour
{
	/// <summary>
	/// get singleton instance.
	/// </summary>
	public static wsAudioManager Instance
	{
		get { return wsSingletonObject<wsAudioManager>.Instance; }
	}

	/// <summary>
	/// sound clip list.
	/// </summary>
	public wsAudioClip[] sounds;

	/// <summary>
	/// play main sound for bgm.
	/// </summary>
	/// <param name="name"></param>
	/// <param name="loop"></param>
	public void Play(string name, bool loop)
	{
		audio.clip = GetClip(name);
		audio.loop = loop;
		audio.Play();
	}

	/// <summary>
	/// stop main sound.
	/// </summary>
	public void Stop()
	{
		audio.Stop();
	}

	/// <summary>
	/// pause main sound.
	/// </summary>
	public void Pause()
	{
		audio.Pause();
	}

	/// <summary>
	/// play audio clip.
	/// </summary>
	/// <param name="name"></param>
	public void PlayOneShot(string name)
	{
		audio.PlayOneShot(GetClip(name));
	}

	/// <summary>
	/// get audio clip.
	/// </summary>
	/// <param name="name"></param>
	/// <returns></returns>
	private AudioClip GetClip(string name)
	{
		foreach (wsAudioClip one in sounds)
		{
			if (one.name == name)
				return one.clip;
		}

		Debug.LogError("name of sound is not matching.");
		return null;
	}
}
